<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;
use app\admin\model\Users;
use app\admin\model\News;
use app\admin\model\cash\Rechargelog;
use app\admin\model\cash\Refundlog;
use app\admin\model\cash\Activelog;



/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取APP配置信息
     *
     */
    public function index()
    {
        $noticeText=Db::name('config')->where('name','noticeText')->value('value');//滚动通知
        $phone=Db::name('config')->where('name','phone')->value('value');//客服电话
        $maxNumber=Db::name('config')->where('name','maxNumber')->value('value');//每天互动最大次数
        $walletUrl=Db::name('config')->where('name','walletUrl')->value('value');//充值钱包地址
        $newsList=News::all();//公告列表
        $queuePerson=Db::name('users')->where('status',0)->count();//排队人数
        $queueCash=Db::name('users')->where('status',0)->sum('usdt');//排队资金
        $data=[
            'noticeText'=>$noticeText,
            'phone'=>$phone,
            'maxNumber'=>$maxNumber,
            'walletUrl'=>$walletUrl,
            'newsList'=>$newsList,
            'queuePerson'=>$queuePerson,
            'queueCash'=>$queueCash
        ];
        //$noticeText=json($noticeText);
        //dump($data);
        $this->success('',$data);
    }
    /**
     * 充值申请
     **/
    public function recharge()
    {
        $data=Request::instance()->param();
        $instance=new Rechargelog($data);
        $instance->allowField(true)->save();
        $this->success('recharge success!');
    }
    /**
     * 退款申请
     **/
     public function refund()
    {
        $data=Request::instance()->param();
        $instance=new Refundlog($data);
        $instance->allowField(true)->save();
        $this->success('refund success!');
    }
     /**
     * 互动
     **/
     public function active()
    {
        $data=Request::instance()->param();
        $users_id=Request::instance()->param('users_id');
        $fee=Db::name('config')->where('name','fee')->value('value');//每次互动提成比例
        $fee1=Db::name('config')->where('name','fee1')->value('value');//一代提成比例
        $fee2=Db::name('config')->where('name','fee2')->value('value');//二代提成比例
        $this->assetIncrease($users_id,$fee,$fee1,$fee2);
        $this->success('active success!');
    }
    /**
     * 获取团队列表
     **/
     public function getTeam()
    {
        $users_id=Request::instance()->param('users_id');
        $data['team1']=Users::all(['pid1'=>$users_id]);
        $data['team2']=Users::all(['pid2'=>$users_id]);
        $this->success('success',$data);
    }
     /**
     * 获取充值记录
     **/
     public function getRechargeLog()
    {
        $data=Request::instance()->param();
        $users_id=Request::instance()->param('users_id');
        $rechargeData=Rechargelog::all(['users_id'=>$users_id]);
        $this->success('success',$rechargeData);
    }
     /**
     * 获取提现记录
     **/
     public function getRefundLog()
    {
        $data=Request::instance()->param();
        $users_id=Request::instance()->param('users_id');
        $refundData=Refundlog::all(['users_id'=>$users_id]);
        $this->success('success',$refundData);
    }
    /**
     * 登陆
     **/
     public function login()
    {
        $data=Request::instance()->param();
        $mobile=Request::instance()->param('mobile');
        $password=Request::instance()->param('password');
        $userInfo=Users::get(['mobile'=>$mobile]);
        if(!$userInfo){
            $this->error('用户没有注册');
        }elseif($userInfo->password!==$password){
            $this->error('密码错误');
        }else{
            $this->success('',$userInfo);
        }
        
    }
    /**
     * 注册
     **/
     public function register()
    {
        $data=Request::instance()->param();
        $mobile=Request::instance()->param('mobile');
        $userInfo=Users::get(['mobile'=>$mobile]);
        if($userInfo){
            $this->error('该手机号已注册');
        }else{
            $pid2=Users::get($data['code'])?Users::get($data['code'])->pid1:0;
            $users=new Users;
            $users->mobile=$mobile;
            $users->password=$data['password'];
            $users->pid1=$data['code'];
            $users->pid2=$pid2;
            $users->nick=$data['nick'];
            $users->create_time=time();
            $users->save();
            $userInfo=Users::get($users->id);
            $this->success('register success',$userInfo);
        }
        
    }
    /**
     * 修改用户信息
     **/
     public function changeProfile()
    {
        $data=Request::instance()->param();
        $users_id=Request::instance()->param('users_id');
        $users=Users::get($users_id);
        $users->allowField(true)->save($data);
        $this->success('change success');
    }
    /**
     * 修改排队状态
     **/
     public function changeQueue()
    {
        
        $personLimit=Db::name('config')->where('name','personLimit')->value('value');
        $queueUsers=Users::where('status',0)->order('id asc')->limit($personLimit)->select();//排队用户
        foreach ($queueUsers as $key => $value) {
            Users::update(['id'=>$value['id'],'status'=>1]);//更改排队状态
        }
        $this->success('change success');
    }
    /**
     * 本人、父亲、祖父资产增长方法
     * @users_id 本人ID
     * @fee 本人增长率
     * @fee1 父亲增长率
     * @fee2 祖父增长率
     **/
     public function assetIncrease($users_id='',$fee=0.08,$fee1=0.04,$fee2=0.02)
    {
        $users=Users::get($users_id);//本人用户
        $father=Users::get($users->pid1);
        $grandfather=Users::get($users->pid2);
        $users->usdt=$users->usdt*(1+$fee);//互动之后的资产
        $users->save();
        if($father){
            $father->usdt=$father->usdt*(1+$fee1);//互动之后的资产
            $father->save();
            if($grandfather){
                $grandfather->usdt=$grandfather->usdt*(1+$fee2);//互动之后的资产
                $grandfather->save();
            }
        }
       
        
        
    }

    /**
     * 测试代码
     **/
    public function test(){
        //$data=Db::name('config')->where('name','teamFee')->value('value');
        $data='["name1","name2"]';
        $data=json_decode($data);
        //$data=array_values($data);
        dump($data);
    }
    
}
